package com.xb.p24_servicelife;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity{

    TextView info=null;
    Intent intent=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        info=findViewById(R.id.tv_msg);

        Button btn_start1=findViewById(R.id.btn_start1);
        Button btn_stop=findViewById(R.id.btn_stop);
        Button btn_start2=findViewById(R.id.btn_start2);

        //绑定监听事件
        btn_start1.setOnClickListener(new MyClick());
        btn_stop.setOnClickListener(new MyClick());
        btn_start2.setOnClickListener(new MyClick());

        //启动服务
        intent=new Intent(this,MyService.class);
    }


    class MyClick implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_start1:
                    intent.putExtra("music","R.raw.by21");
                    MainActivity.this.startService(intent);
                    break;
                case R.id.btn_stop:
                    MainActivity.this.stopService(intent);
                    break;
                case R.id.btn_start2:
                    intent.putExtra("music","R.raw.by22");
                    MainActivity.this.startService(intent);
                    break;
            }
        }
    }
}